 package com.stardata.liangshisq.tcslmon.config;

 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.module.SimpleModule;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
 import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
 import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
 import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
 import com.stardata.liangshisq.tcslmon.interceptor.AuthorizationInterceptor;
 import com.stardata.liangshisq.tcslmon.resolver.LoginUserHandlerMethodArgumentResolver;
 import com.stardata.liangshisq.tcslmon.util.JSONUtil;
 import lombok.AllArgsConstructor;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.http.CacheControl;
 import org.springframework.http.converter.HttpMessageConverter;
 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.format.DateTimeFormatter;
 import java.util.List;
 import java.util.concurrent.TimeUnit;

 @Configuration
 @EnableWebMvc
 @AllArgsConstructor
 public class WebMvcConfig implements WebMvcConfigurer {
  private final AuthorizationInterceptor authorizationInterceptor;
  private final LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;

   public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = JSONUtil.jsonMapper();
 
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    objectMapper.registerModule(simpleModule);
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
 
    objectMapper.registerModule(new Jdk8Module());
    JavaTimeModule module = new JavaTimeModule();
    module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(
      DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(
      DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    module.addDeserializer(LocalDate.class, new LocalDateDeserializer(
      DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    module.addSerializer(LocalDate.class, new LocalDateSerializer(
      DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(
      DateTimeFormatter.ofPattern("HH:mm")));

    module.addSerializer(LocalTime.class, new LocalTimeSerializer(
      DateTimeFormatter.ofPattern("HH:mm")));

    objectMapper.registerModule(module);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    objectMapper.setDateFormat(format);

    jackson2HttpMessageConverter.setObjectMapper(objectMapper);
    converters.add(jackson2HttpMessageConverter);
   }

//  @Override
//  public void addCorsMappings(CorsRegistry registry) {
//   registry.addMapping("/**")
//       .allowedMethods("*")
//       .allowedHeaders("*")
//       .maxAge(1800)
//       .allowedOrigins(feServerProperties.getUrl());
//   }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
//   registry.addInterceptor(authorizationInterceptor)
//       .addPathPatterns("/**")
//       .excludePathPatterns("/static/*")
//       .excludePathPatterns("/swagger-ui.html")
//       .excludePathPatterns("/webjars/**");
   registry.addInterceptor(authorizationInterceptor)
       .addPathPatterns("/v1/**").addPathPatterns("/v2/**");
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
   registry.addResourceHandler("/static/**")
       .addResourceLocations("classpath:/static/")
       .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
   // 解决 SWAGGER 404报错
   registry.addResourceHandler("/swagger-ui.html")
       .addResourceLocations("classpath:/META-INF/resources/");
   registry.addResourceHandler("/webjars/**")
       .addResourceLocations("classpath:/META-INF/resources/webjars/");
  }


  @Override
  public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
    resolvers.add(loginUserHandlerMethodArgumentResolver);
  }

 }
